//
//  YLDictionaryExtension.swift
//  iosh5vue
//
//  Created by lyl on 2021/3/1.
//  Copyright © 2021 lyl. All rights reserved.
//

import Foundation
extension Dictionary {
    func int(_ key: String, _ defaultValue: Int) -> Int {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return (data[key]! as? Int) ?? defaultValue
            }
        }
        return defaultValue
    }
    func int(_ key: String) -> Int {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return Int("\(data[key]!)") ?? 0
            }
        }
        return 0
    }
    func string(_ key: String) -> String {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return "\(data[key]!)"
            }
        }
        return ""
    }
    func string(_ key: String, _ defaultValue: String) -> String {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            if data.keys.contains(key) && "\(data[key]!)" != "<null>" && "\(data[key]!)" != "" {
                return "\(data[key]!)"
            }
        }
        return defaultValue
    }
    func dictionary(_ key: String) -> [String : Any] {
        let dictionary = self as? Dictionary<String, Any>
        if let data = dictionary {
            let content = (data[key] as? Dictionary<String, Any>)
            if content != nil {
                return content!
            }else {
                return [String : Any]()
            }
        }
        return [String : Any]()
    }
    func array(_ key: String) -> [[String : Any]] {
        let array = self as? [String : Any]
        //MyLog(array)
        if let data = array {
            let content = (data[key] as? [[String : Any]])
            if content != nil { return content! }
            else { return [[String : Any]]() }
        }
        return [[String : Any]]()
    }
    func dictionaryArray(_ key: String) -> [[String : Any]] {
        let dict = self as? [String : Any]
        if let data = dict {
            let content = (data[key] as? [[String : Any]])
            if content != nil { return content! }
            else { return [[String : Any]]() }
        }
        return [[String : Any]]()
    }
    func stringArray(_ key: String) -> [String] {
        let array = self as? [String : Any]
        if let data = array {
            let content = (data[key] as? [String])
            if content != nil { return content! }
            else { return [String]() }
        }
        return [String]()
    }
    func intArray(_ key: String) -> [Int] {
        let array = self as? [String : Any]
        if let data = array {
            let content = (data[key] as? [Int])
            if content != nil { return content! }
            else { return [Int]() }
        }
        return [Int]()
    }
    
    func json() -> String {
        guard JSONSerialization.isValidJSONObject(self) else { return "{}" }
        guard let data = try? JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) else { return "{}" }
        guard let jsonString = String(data: data, encoding: .utf8)?.replacingOccurrences(of: "\\/", with: "/") else { return "{}" }
        return jsonString
    }
}
